package com.base.myproject.client.HR.attendance;

import java.util.ArrayList;
import java.util.List;

import com.base.myproject.client.busi.BO.BusiObject;
import com.base.myproject.client.jdbc.DataSet;
import com.base.myproject.client.system.Env;
import com.base.myproject.client.tools.Tools;
import com.base.myproject.client.ui.BaseJDBCPanel;
import com.base.myproject.client.ui.BaseOpenJSP;
import com.base.myproject.client.ui.celleditor.BaseCheckGroup;
import com.base.myproject.client.ui.celleditor.BaseComboBoxForm;
import com.base.myproject.client.ui.celleditor.BaseComboBoxX;
import com.extjs.gxt.ui.client.event.BaseEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.widget.HorizontalPanel;
import com.extjs.gxt.ui.client.widget.Label;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.DateField;
import com.google.gwt.core.client.GWT;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.VerticalPanel;
/**
 * 考勤异常报表
 * @author mzba
 *
 */
public class RptAttendanceException extends BaseJDBCPanel{

	private BaseOpenJSP jspPanel = null;
	private String jspFile = null;
	private DateField dfBegin = null;
	private DateField dfEnd = null;
	private Button btnQuery = null;
	private BaseComboBoxForm<BusiObject> department = null;
	private BaseComboBoxX manager = null;
	private String bcode = Env.getEnvbcode();
	private String results = null;
	private List<String> level = null;
	private BaseCheckGroup check = null;
	
	
	public RptAttendanceException(){
		/**
		 * Get the permissions for employer who's login
		 */
		String sql = "select substring('id like '''+replace(permissionsbcode,';','%25'' or id like '''),9," +
		"len('id like '''+replace(permissionsbcode,';','%25'' or id like '''))-21) as result from" +
		" busipermissions where bcode = '"+bcode+"' and busicode = 8001";
		final com.base.myproject.client.tools.GreetingServiceAsync greetingService2 = GWT.create(com.base.myproject.client.tools.GreetingService.class);
		greetingService2.getDataSet(sql, new AsyncCallback<DataSet>(){
		@Override
			public void onFailure(Throwable caught) {	
			}
			@Override
			public void onSuccess(DataSet result) {  
				if(result.getRowCount() != 0){
					results = result.getValue(0, "result");
					/**
					 * Get the employlevel from database for BaseCheckGroup
					 */
					String sql2 = "select name,type from dictionary where type='zy'";
					final com.base.myproject.client.tools.GreetingServiceAsync greetingService2 = GWT.create(com.base.myproject.client.tools.GreetingService.class);
					greetingService2.getDataSet(sql2, new AsyncCallback<DataSet>(){
					@Override
						public void onFailure(Throwable caught) {	
						}
						@Override
						public void onSuccess(DataSet result) {  
							if(result.getRowCount() != 0){
								level = new ArrayList<String>();
								for(int i = 0;i < result.getRowCount();i++){
									level.add(result.getValue(i, "name"));
								}
							}
							init();
						}
					});
				}else{
					MessageBox.alert("提示", "抱歉，您没有查看权限", null);
				}
			}
		});
	}
	
	public void init(){
		initWidget();
	}
	
	public void complete() {
		super.complete();
		print.addListener(Events.Select, getListener());
		getToolbarll().add(print);
	}
	
	public void initWidget(){
		jspFile = "./hr_Jsp/rptAttendanceException.jsp";
		jspPanel = new BaseOpenJSP(jspFile);
		Tools.setResize(jspPanel, 0.99, 0.9);
		jspPanel.getFrame().getElement().setAttribute("frameborder", "0");
		
		HorizontalPanel hp = new HorizontalPanel();
		hp.setSpacing(3);
		department = new BaseComboBoxForm<BusiObject>("id","name","Systeminfo","where id like "+results+" order by id asc",true);
		manager = new BaseComboBoxX("name", "dictionary", "where type = 'attendance'");
		dfBegin = new DateField();
		dfBegin.getPropertyEditor().setFormat(DateTimeFormat.getFormat("yyyy-MM-dd"));
		dfEnd = new DateField();
		dfEnd.getPropertyEditor().setFormat(DateTimeFormat.getFormat("yyyy-MM-dd"));
		btnQuery = new Button("查询");
		btnQuery.addListener(Events.OnClick, getListener());
		String title[] = level.toArray(new String[level.size()]);
		check = new BaseCheckGroup(title,true);
		check.setHeight(30);
		
		hp.add(new Label("部门："));
		hp.add(department);
		department.setWidth(127);
		hp.add(new Label("雇佣级别:"));
		hp.add(check);
		
		hp.add(new Label("处理情况："));
		hp.add(manager);
		manager.setWidth(80);
		hp.add(new Label("从:"));
		hp.add(dfBegin);
		dfBegin.setWidth(100);
		hp.add(new Label("到:"));
		dfEnd.setWidth(100);
		hp.add(dfEnd);
		hp.add(btnQuery);
		
		VerticalPanel vp = new VerticalPanel();
		vp.setSpacing(3);
		vp.add(hp);
		vp.add(jspPanel);
		add(vp);
		this.layout();
	}
	
	int sync = 0;                                                      
	Listener<BaseEvent> defaultlistener;                               
                                                                     
	public Listener<BaseEvent> getListener() {                                
		if(defaultlistener!=null)                                        
		{                                                                
			return defaultlistener;                                        
		}                                                                
                                                                     
		defaultlistener = new Listener<BaseEvent>() {                    
			public void handleEvent(BaseEvent be) {                        
				sync++;                                                    
				try {                                                      
					if (sync == 1) {                                   
						if (be.getType() == Events.Select) { 
							jspPanel.Print();
						}else if(be.getType() == Events.OnBlur){
							
						}else if(be.getType() == Events.OnClick){
							if(be.getSource().equals(btnQuery)){
								String depCode = department.getValueCode();
								String beginTime = dfBegin.getRawValue();
								String endTime = dfEnd.getRawValue();
								String managerValue = manager.getRawValue();
								String level = "";
								if(check.getValueArray().length != 0){
									for(int i = 0;i < check.getValueArray().length;i++){
										String value = (String)check.getValueArray()[i];
										level += "'" + value + "'" + ",";
									}
									level = level.substring(0, level.length() - 1);
								}
								if(beginTime.equals("") || endTime.equals("") || depCode.equals("")){
									MessageBox.alert("提示", "查询信息填写不完整", null);
								}else{
									String depName = department.getValue().get("name");
									jspFile += "?beginTime=" + beginTime + "&endTime=" + endTime  + "&depCode=" + depCode + "&manager=" + managerValue + "&department=" + depName + "&level=" + level;
									jspPanel.getFrame().setUrl(jspFile);
									jspFile = "./hr_Jsp/rptAttendanceException.jsp";
									
								}
							}
						}                                          
					}                                                  
				} finally {                                                
					sync--;                                            
				}                                                          
			}                                                              
		};                                                               
		return defaultlistener;                                          
	}
}
